package cn.com.paycn.ken.utils;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Random;
import javax.imageio.ImageIO;

public class AuthImageUtils
{
  public static final short BRIGHT_DEFAULT = 0;
  public static final short BRIGHT_HIGHT = 1;
  public static final short BRIGHT_LOW = 2;
  public static final short BRIGHT_ALL = 3;
  private static char[] codeSequence = null;

  private int width = 84;
  private int height = 34;
  private int codeCount = 4;
  private int fontHeight = 30;
  private int x = 15;
  private int y = 28;
  private String typeface = "Fixedsys";
  private short bright = 0;
  private String formatName = "jpeg";
  private BufferedImage buffImg;
  private Random random = new Random();

  private StringBuffer authCode = new StringBuffer("");

  static {
    codeSequence = "ABCDEFGHiJKLMNOPQRSTUVWXYZ1234567890".toCharArray();
  }

  private AuthImageUtils(int width, int height)
  {
    this.width = width;
    this.height = height;
    this.buffImg = new BufferedImage(width, height, 1);
  }

  public static AuthImageUtils newInstance(int width, int height)
  {
    return new AuthImageUtils(width, height);
  }

  public String getAuthCode()
  {
    return this.authCode.toString();
  }

  public void setFont(String font)
  {
    this.typeface = font;
  }

  public void setImageFormat(String formatName)
  {
    this.formatName = formatName;
  }

  public void setBrightness(short bright)
  {
    if ((bright == 1) || (bright == 2) || (bright == 3))
      this.bright = bright;
  }

  private Color getRandomColor(short bright)
  {
    int red = 0; int green = 0; int blue = 0;

    if (this.bright != 0) {
      bright = this.bright;
    }

    switch (bright) {
    case 1:
      red = 255 - this.random.nextInt(130);
      green = 255 - this.random.nextInt(130);
      blue = 255 - this.random.nextInt(130);
      break;
    case 2:
      red = this.random.nextInt(130);
      green = this.random.nextInt(130);
      blue = this.random.nextInt(130);
      break;
    case 3:
      red = this.random.nextInt(255);
      green = this.random.nextInt(255);
      blue = this.random.nextInt(255);
    }

    return new Color(red, green, blue);
  }

  private Graphics getGraphics()
  {
    Graphics gd = this.buffImg.getGraphics();

    gd.setColor(Color.WHITE);
    gd.fillRect(0, 0, this.width, this.height);

    gd.setFont(new Font(this.typeface, 1, this.fontHeight));

    gd.setColor(Color.BLACK);
    gd.drawRect(0, 0, this.width - 1, this.height - 1);

    gd.setColor(Color.BLACK);
    for (int i = 0; i < 40; i++) {
      int x = this.random.nextInt(this.width);
      int y = this.random.nextInt(this.height);
      int xl = this.random.nextInt(12);
      int yl = this.random.nextInt(12);
      gd.drawLine(x, y, x + xl, y + yl);
    }
    return gd;
  }

  private void append(Graphics gd, int x, int y, short bright)
  {
    String code = String.valueOf(codeSequence[this.random.nextInt(codeSequence.length)]);

    gd.setColor(getRandomColor(bright));
    gd.drawString(code, x, y);

    this.authCode.append(code);
  }

  public void output(OutputStream out) throws IOException {
    Graphics gd = getGraphics();

    for (int i = 0; i < this.codeCount; i++) {
      if ((i + 1) % 2 == 0)
        append(gd, (i + 1) * this.x, this.y, (short)1);
      else
        append(gd, (i + 1) * this.x, this.y, (short)2);
    }
    try
    {
      ImageIO.write(this.buffImg, this.formatName, out);
    } finally {
      if (out != null)
        out.close();
    }
  }

  public static void main(String[] args) throws IOException {
    AuthImageUtils i = newInstance(84, 34);

    FileOutputStream out = new FileOutputStream("F:/aaa/a.jpg");
    i.output(out);
  }
}